var util = require('../../../utils/util.js');
var indx = 0;
var player;
let videoContext;
let videoContexta;
let isPlay = true;
let page = 1;
let pageSize = 10
Page({
  data: {
    x:0,y:0,
    lastTapTime:0,
    loveHidden:true,
    //以下 弹出层
    showModalStatus: false,
    animationData: '',
    isLoading:false,
    visable:true,
    // 以上弹出层
    top:"",
    statusBarHeight:"",
    weishipinglist: [],
      //{
      // "id":1,
      //   "videoid": "http://image.bangnila.com/30195bd3e79a267c28f4328e5a0c3c9d.mp4",
      //   "commentNum":101,
      //   "shareNum":1001,
      //   "dianzanNum":10101,
      //   "author":"某某人1",
      //   "content":"关于这个视频，有很多知识点需要复习1,阿三打撒打撒打撒打撒大苏打撒旦撒萨达萨达",
      //   "userId":10,
      //   "dianzanColor":0
      // },
      // {
      //   "id":2,
      //   "videoid": "http://image.bangnila.com/马句 - 一生啊.mkv",
      //   "commentNum":102,
      //   "shareNum":1002,
      //   "dianzanNum":10102,
      //   "author":"某某人2",
      //   "content":"关于这个视频，有很多知识点需要复习2，啊飒飒撒啊飒飒飒飒倒萨倒萨大苏打阿斯顿撒旦撒",
      //   "userId":10,
      //   "dianzanColor":1
      // },
    hkindex: 0, //滑块index
    videoInfo:"",
    dianzan:false,
    content:"",
    commentList:[]
  },
  playThis:function(){
    videoContext.play();
    this.setData({
      visable:true
    })
  },
  onLoad:function(options){
    this. init()
    indx = 0;
     player = wx.createVideoContext("myvideo" +indx + "");
    console.log(wx.getMenuButtonBoundingClientRect().height)
    console.log(wx.getSystemInfoSync().statusBarHeight)
    var top = wx.getSystemInfoSync().statusBarHeight + (wx.getMenuButtonBoundingClientRect().top - 32)/2;
    this.setData({
      top: top,
      statusBarHeight:wx.getMenuButtonBoundingClientRect().height,
      visable:true
    })
    console.log(this.data.weishipinglist[0]);
    videoContext = wx.createVideoContext("myvideo" + 0 + "")
  },
  init(){
    this.getVideoList()
  },
  getVideoList:function(){
    var that = this;
    var param={
      page:page,
      pageSize:pageSize
    }
    util.request("platShortVideoAction.getShortVideo",param).then(function(res){
      console.log(res)
      that.setData({
        weishipinglist:res.body.list,
        videoInfo:res.body.list[0]
      })
    })
  },
  onReady:function(){
    //进入页面开始播放
    player.play();
  },
  //动态更新当前滑块下标
  bindchange(e) {
    console.log("点击进入方法了")
    this.setData({
      hkindex: e.detail.current
    })
    console.log(e)   
    videoContext = wx.createVideoContext("myvideo" + e.detail.current + "")
    videoContexta = wx.createVideoContext("myvideo" + indx + "")
    indx  =e.detail.current
    videoContexta.pause();
    videoContexta.seek(0);
    videoContext.play();
     this.setData({
       videoInfo:this.data.weishipinglist[e.detail.current],
       visable:true
     })
    let xz = this.data.weishipinglist.length - e.detail.current;
    console.log("是否满足加载"+xz)
    if (xz == 3) {
      this.addmore(); //加载更多视频
    }
   // this.qingygnanme();
  },
  addmore:function(){
    var that =this
     page = page+1;
     var param={
      page:page,
      pageSize:pageSize
     }
     util.request("platShortVideoAction.getShortVideo",param).then(function(res){
      console.log(res)
      var arr = that.data.weishipinglist;
      var ar = res.body.list;
      for(var i=0;i<ar.length;i++){
      var ar = res.body.list;
        arr.push(ar[i])
      }
      that.setData({
        weishipinglist:arr
      })
     })
  },

  back:function(){
    wx.navigateBack()
  },
  backhome:function(){
     wx.reLaunch({
       url: '/pages/index/index',
     })
  },
  comment: function () {
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "ease-in-out",
      delay: 0
    })
    this.animation = animation
    animation.translateY(500).step()
    this.setData({
      animationData: animation.export(),
      showModalStatus: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
    this.getList()
  },
  hideModal: function () {
    this.setData({
      showModalStatus: false,
    })
  },
  toLow(){
    console.log("加载更多")
    this.setData({
      isLoading: true
    })
  },
  //点赞模块
  dianzan:function(e){
  var that = this;
  console.log(e)
  console.log(that.data.hkindex)
  var video = that.data.weishipinglist[that.data.hkindex]
  if(video.dianzanColor==1){
    video.dianzanColor=0,
    video.dianzanNum--;
  }else{
    video.dianzanColor=1
    video.dianzanNum++;
  }
  that.setData({
    videoInfo:video
  })
  },
  //分享
  share:function(e){
    var that = this
  //  console.log(e)
    that.onShareAppMessage(e)
  },
  onShareAppMessage: function(e){
    var that =this
    console.log("asasa")
    console.log( that.data.videoInfo)
    var content =  that.data.videoInfo.content;
    var poster = that.data.videoInfo.poster
    return {
       title:content,
      // imageUrl:poster,
       path: '/shortVideo/pages/short/index',
   }
},
getinput:function(e){
  var that =this
  console.log(e.detail.value)
  that.setData({
    content:e.detail.value
  })
},
publish:function(e){
  console.log("sasasa")
  var that =this
 var param = {
 content: that.data.content,
  targetId: e.currentTarget.dataset.id
};
if(that.data.content==null){
  wx.showToast({
    title: '评论内容不能为空',
  })
return;
}
util.request("platCommentAction.comment", param).then(function(res){
  that.getList()
})
},

getList: function () {
  var that = this;
  console.log("加载")
  var param = {
    page: page,
    pageSize: pageSize,
    targetId: that.data.videoInfo.id
  }
  util.request("platCommentAction.getCommentList", param).then(function (res) {
    console.log(res)
    var list = res.body.list;
    that.setData({
      commentList: list
    })
  })
},
// 屏幕操做
touchStartPoint: [0, 0],  // 触摸开始坐标 (屏幕左上角为坐标系原点, 向右为x轴, 向下为y轴)
touchEndPoint: [0, 0],    // 触摸结束坐标 (屏幕左上角为坐标系原点, 向右为x轴, 向下为y轴)
touchStartTime: 0,   // 触摸开始时间
touchEndTime: 0,     // 触摸结束时间
lastTapTime: 0,  // 最后一次单击事件点击发生时间
lastTapTimeoutFunc: null, // 单击事件点击后要触发的函数
// 触摸开始
touchStart: function(e){
  console.log('touch start')
  console.log('坐标x: ' + e.touches[0].pageX)
  console.log('坐标y: ' + e.touches[0].pageY)
  let sx = e.touches[0].pageX
  let sy = e.touches[0].pageY
  this.touchStartPoint = [sx, sy] //坐标
  this.touchStartTime = e.timeStamp //时间点
},

// 触摸结束
touchEnd: function (e) {
  console.log('touch end')
  //注意:触摸结束没有坐标监听事件,故读不到坐标点
  this.touchEndTime = e.timeStamp //时间点
},
// 长按tap
longTap: function (e) {
  console.log("long tap")
  wx.showModal({
    title: '提示',
    content: '长按事件被触发',
    showCancel: false
  })
},

//单击tap或双击tap
multipleTap: function(e){
  console.log(e)
  var that = this
  let curTime = e.timeStamp;
  let lastTime = this.lastTapDiffTime;
  this.lastTapDiffTime = curTime;
    //两次点击间隔小于300ms, 认为是双击
    let diff = curTime - lastTime;
    if (diff < 300) {
      console.log("double tap")
      clearTimeout(this.lastTapTimeoutFunc); // 成功触发双击事件时，取消单击事件的执行
      var videoInfo = that.data.weishipinglist[that.data.hkindex];
      if( videoInfo.dianzanColor==0){
        videoInfo.dianzanColor = 1;
        videoInfo.dianzanNum ++;
      }
      console.log(videoContexta)
       that.setData({
        videoInfo:videoInfo,
        x:e.detail.x,
        y:e.detail.y,
        loveHidden:false
       })
       //爱心动画
       var animation1 =wx.createAnimation({
        duration: 0,
        timingFunction: 'ease',
        delay: 0
       })
       animation1.opacity(1).scale(1).step();
       that.setData({
        ani:  animation1.export()
      })
       var animation =wx.createAnimation({
        duration: 2000,
        timingFunction: 'ease',
        delay: 100
       })
       animation.opacity(0.0).translateY(-120).scale(1.35).step();
       that.setData({
        ani:  animation.export()
      })
      //  //点赞“加”请求后台
    } else {
      // 单击事件延时300毫秒执行，这和最初的浏览器的点击300ms延时有点像。
      this.lastTapTimeoutFunc = setTimeout(function () {
        console.log("single tap")
         if(isPlay){
          videoContext.pause();
          isPlay = false
          that.setData({
            visable:false
          })
         }else{
          videoContext.play();
          isPlay = true
          that.setData({
            visable:true
          })
         }
      }, 300);
    }
},


//滑动
touchMove: function (e) {
  // let start = this.touchStartPoint[0]
  // let end = this.touchEndPoint[0]
  console.log('move');
  console.log('坐标x: ' + e.touches[0].pageX)
  console.log('坐标y: ' + e.touches[0].pageY)
}
})